#include "controlcan.h"

int main()
{
    int devType = VCI_USBCAN2;   // 双通道
    int devIdx  = 0;             // 第 1 个设备
    int canIdx0 = 0;             // CAN 通道 0
    int canIdx1 = 1;             // CAN 通道 1

    // 1. 打开设备
    if (VCI_OpenDevice(devType, devIdx, 0) != STATUS_OK) {
        printf(">>open device error!\n");
        return -1;
    }
    printf(">>open device success!\n");

    // 2. 初始化 CAN 通道配置
    VCI_INIT_CONFIG config;
    config.AccCode = 0;
    config.AccMask = 0xFFFFFFFF;
    config.Filter  = 1;
    config.Timing0 = 0x00;   // 根据波特率设置
    config.Timing1 = 0x1C;   // 500kbps
    config.Mode    = 0;      // 正常模式

    // 3. 初始化 CAN0
    if (VCI_InitCAN(devType, devIdx, canIdx0, &config) != STATUS_OK) {
        printf(">>Init CAN0 error\n");
        return -1;
    }
    printf(">>Init CAN0 success\n");

    // 4. 启动 CAN0
    if (VCI_StartCAN(devType, devIdx, canIdx0) != STATUS_OK) {
        printf(">>Start CAN0 error\n");
        return -1;
    }
    printf(">>Start CAN0 success\n");

    // 5. 初始化 CAN1
    if (VCI_InitCAN(devType, devIdx, canIdx1, &config) != STATUS_OK) {
        printf(">>Init CAN1 error\n");
        return -1;
    }
    printf(">>Init CAN1 success\n");

    // 6. 启动 CAN1
    if (VCI_StartCAN(devType, devIdx, canIdx1) != STATUS_OK) {
        printf(">>Start CAN1 error\n");
        return -1;
    }
    printf(">>Start CAN1 success\n");

    // 7. 读取板卡信息
    VCI_BOARD_INFO boardInfo;
    if (VCI_ReadBoardInfo(devType, devIdx, &boardInfo) == STATUS_OK) {
        printf(">>USB CAN Board SN: %s\n", boardInfo.str_Serial_Num);
    } else {
        printf(">>Get VCI_ReadBoardInfo error!\n");
    }

    return 0;
}
